﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using System.Data.SqlClient;
using League.DataAccess;
using System.Data;
using League.DBUtility;
using System.Reflection;
using JJY.DBUtility;
using JJY.CommonHelper;

namespace League.Business
{
    public class TeamAllDataController
    {
        public static bool InsertFutureMatch(string leagueName, short seasonYear, DateTime matchDateTime, 
            byte roundNumber, string homeTeamName, string visitingTeamName)
        {
            bool result = false;

            //league insert, and get leagueID
            LeagueInfo leagueInfo = new LeagueInfo();
            leagueInfo.LeagueName = leagueName;
            leagueInfo.SeasonYear = seasonYear;
            Guid leagueID = Guid.Parse(LeagueInfoDataAccess.Insert(leagueInfo));

            //team name insert, and get teamIDs
            Dictionary<string, string> dicTeamName =
                LeagueTeamDataAccess.BatchInsert(leagueID.ToString(), homeTeamName, visitingTeamName);

            LeagueMatch leagueMatch = new LeagueMatch();
            //match insert, and get matchID
            leagueMatch.HomeTeamID = dicTeamName[homeTeamName].ToGuid();
            leagueMatch.VisitingTeamID = dicTeamName[visitingTeamName].ToGuid();
            leagueMatch.LeagueID = leagueID;
            leagueMatch.MatchDateTime = matchDateTime;
            leagueMatch.RoundNumber = roundNumber;
            leagueMatch.MatchResult = 4;

            Guid matchID = LeagueMatchDataAccess.Insert(leagueMatch).ToGuid();

            return result;
        }

        public static bool Insert(DateTime matchDateTime, LeagueMatch leagueMatch, LeagueInfo leagueInfo, 
            LeagueTeamData homeTeam, LeagueTeamData visitingTeam, LeagueMatchStatsHtml leagueMatchStatHtml)
        {
            bool result = false;

            try
            {
                //league insert, and get leagueID
                Guid leagueID = Guid.Parse(LeagueInfoDataAccess.Insert(leagueInfo));

                //team name insert, and get teamIDs
                Dictionary<string, string> dicTeamName =
                    LeagueTeamDataAccess.BatchInsert(leagueID.ToString(), homeTeam.TeamName,visitingTeam.TeamName);

                //match insert, and get matchID
                leagueMatch.HomeTeamID = dicTeamName[homeTeam.TeamName].ToGuid();
                leagueMatch.VisitingTeamID = dicTeamName[visitingTeam.TeamName].ToGuid();
                leagueMatch.LeagueID = leagueID;
                leagueMatch.MatchDateTime = matchDateTime;
                if (homeTeam.Goal > visitingTeam.Goal)
                {
                    leagueMatch.MatchResult = 3;
                }
                else if (homeTeam.Goal < visitingTeam.Goal)
                {
                    leagueMatch.MatchResult = 0;
                }
                else
                {
                    leagueMatch.MatchResult = 1;
                }
                Guid matchID = LeagueMatchDataAccess.Insert(leagueMatch).ToGuid();

                //teamdata insert
                homeTeam.MatchID = matchID;
                homeTeam.IsAtHome = true;
                homeTeam.TeamID = dicTeamName[homeTeam.TeamName].ToGuid();
                visitingTeam.MatchID = matchID;
                visitingTeam.IsAtHome = false;
                visitingTeam.TeamID = dicTeamName[visitingTeam.TeamName].ToGuid();
                LeagueTeamDataDataAccess.Insert(homeTeam, visitingTeam);

                LeagueMatchStatsHtmlDataAccess.Insert(leagueMatchStatHtml);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
    }
}
